//
//  UIScrollView+TL_Refresh.m
//  WXReader
//
//  Created by TSL on 2023/7/19.
//  Copyright © 2023 Andrew. All rights reserved.
//

#import "UIScrollView+TL_Refresh.h"

static const char kTitleLabel = '\1';

@implementation UIScrollView (TL_Refresh)

- (void)addHeaderRefreshWithRefreshingBlock:(MJRefreshComponentAction)refreshBlock
{
    __weak typeof(self) weakSelf = self;
    TL_BubblesHeader *header = [TL_BubblesHeader headerWithRefreshingBlock:^{
        weakSelf.mj_footer.state = MJRefreshStateIdle;
        self.titleLabel.hidden = YES;
        if (refreshBlock) {
            refreshBlock();
        }
    }];
    header.adaptX = NO;
    self.mj_header = header;
}

- (void)addHigherHeaderRefreshWithRefreshingBlock:(MJRefreshComponentAction)refreshBlock
{
    TL_BubblesHeader *header = [TL_BubblesHeader headerWithRefreshingBlock:^{
        if (refreshBlock) {
            refreshBlock();
        }
    }];
    header.adaptX = YES;
    self.mj_header = header;
}

- (void)addFooterRefreshWithRefreshingBlock:(MJRefreshComponentAction)refreshBlock
{
    TL_BubblesFooter *footer = [TL_BubblesFooter footerWithRefreshingBlock:^{
        if (refreshBlock) {
            refreshBlock();
        }
    }];
    footer.adaptX = NO;
    UILabel *titleLabel = [[UILabel alloc] init];
    self.titleLabel = titleLabel;
    titleLabel.textColor = kGrayTextColor;
    titleLabel.text = WXLocalizedString(@"没有更多内容了");
    titleLabel.font = SYSTEM_FONT(12);
    [footer addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(footer);
    }];
    titleLabel.hidden = YES;
    self.mj_footer = footer;
}

- (void)addToastFooterRefreshWithRefreshingBlock:(MJRefreshComponentAction)refreshBlock
{
    TL_LoadMoreFooter *footer = [TL_LoadMoreFooter footerWithRefreshingBlock:^{
        if (refreshBlock) {
            refreshBlock();
        }
    }];
    self.mj_footer = footer;
}

- (void)setTitleLabel:(UILabel *)titleLabel {
    objc_setAssociatedObject(self, &kTitleLabel, titleLabel, OBJC_ASSOCIATION_RETAIN);
}

- (UILabel *)titleLabel {
    return objc_getAssociatedObject(self, &kTitleLabel);
}

- (void)showRefreshFooter
{
    if (self.mj_footer.hidden) {
        self.mj_footer.hidden = NO;
    }
    self.titleLabel.hidden = YES;
    self.mj_footer.state = MJRefreshStateIdle;
}

- (void)showRefreshHeader
{
    if (self.mj_header.hidden) {
        self.mj_header.hidden = NO;
    }
}

- (void)hideRefreshFooter
{
    self.titleLabel.hidden = NO;
    [self.mj_footer endRefreshingWithNoMoreData];
    self.mj_footer.state = MJRefreshStateNoMoreData;
    
}
- (void)hideRefreshFooterAndHiddenLab
{
    self.titleLabel.hidden = YES;
    [self.mj_footer endRefreshingWithNoMoreData];
    self.mj_footer.state = MJRefreshStateNoMoreData;
    
}
- (void)hideRefreshHeader
{
    if (!self.mj_header.hidden) {
        self.mj_header.hidden = YES;
    }
}

- (void)endRefreshing
{
    [self.mj_header endRefreshing];
    [self.mj_footer endRefreshing];
}

@end
